package fecat.ui.frame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.plaf.ViewportUI;

import fecat.core.ILanguage;
import fecat.services.language.LanguageService;
import fecat.services.log.LoggingService;
import fecat.ui.contextpanel.ExtendDockableFrame;
import fecat.ui.contextpanel.table.FECatBaseTable;
import fecat.util.SwingResourceManager;

public abstract class BaseFrame extends ExtendDockableFrame implements ILanguage{
	protected JTable desktop;
	protected JScrollPane scrollpane;
	public BaseFrame(String name) {
		super(name);
		this.createScrollPanel();
		this.addComponentListener(this.getComponentListener());
		LanguageService.addLanguageChangeListener(this);
	}
	public void createScrollPanel(){
		try{
			JComponent comp=this.createChildComponent();
			scrollpane = new CustomScrollPane(comp);
			if(comp instanceof FECatBaseTable)
				((FECatBaseTable)comp).addPopup(scrollpane);
//			scrollpane.setBackground(SwingResourceManager.getColor(48,70,118));
//			scrollpane.setAutoscrolls(true);
			this.add(scrollpane);
		}
		catch(Exception ex){
			LoggingService.error(ex);
		}
	}
	public JComponent createChildComponent(){
		return null;
	}
	public ComponentListener getComponentListener(){
		ComponentListener componentListener=(new ComponentListener(){
			public void componentResized(ComponentEvent arg0) {
				Dimension dim=arg0.getComponent().getSize();
				changeComponentSize(dim);
			}

			public void componentMoved(ComponentEvent arg0) {
				
				Dimension dim=arg0.getComponent().getSize();
				changeComponentSize(dim);
			}

			public void componentShown(ComponentEvent arg0) {
				Dimension dim=arg0.getComponent().getSize();
				changeComponentSize(dim);
			}

			public void componentHidden(ComponentEvent arg0) {
				Dimension dim=arg0.getComponent().getSize();
				changeComponentSize(dim);
			}
	    	
	    });
		return componentListener;
	}
	protected void changeComponentSize(Dimension dim) {
//		Dimension size=this.scrollpane.getSize();
//		if(desktop==null)
//			return;
////		if(size.width<700)
////			return;
//		desktop.setPreferredSize(size);
//		desktop.repaint();
	}
}
class CustomScrollPane extends JScrollPane{
	public CustomScrollPane(JComponent comp) {
		super(comp);
//		CustomViewPort cvp=new CustomViewPort();
//		cvp.setUI(new CustomViewportUI());
//		cvp.setView(comp);
//		this.setViewport(cvp);
	}

	public synchronized void paint(Graphics g){
		try{
			JViewport colument=this.getRowHeader();
			JViewport columentHeaderViewpPort=this.getColumnHeader();
			if(columentHeaderViewpPort!=null){
				this.getColumnHeader().setBackground(SwingResourceManager.getColor(171,168,153));
				this.getColumnHeader().getView().setBackground(SwingResourceManager.getColor(171,168,153));
			}
			this.getViewport().setBackground(SwingResourceManager.getColor(171,168,153));
			super.paint(g);
		}
		catch(Exception ex){
			LoggingService.error(ex);
		}
	}
	public void paintComponent(Graphics g){
		
		for(int i=0;i<this.getHeight()/20;i++){
			if(i%2==0){
				g.setColor(SwingResourceManager.getColor(50,29,34));
			}
			else{
				g.setColor(SwingResourceManager.getColor(100,129,33));
			}
			g.fillRect(0,i*20,20,this.getWidth());
		}
		g.setColor(SwingResourceManager.getColor(100,129,33));
		g.fillRect(0,60,200,200);
		super.paintComponent(g);
	}
}
class CustomViewPort extends JViewport{
	
	public void paintComponent(Graphics g){
		
		for(int i=0;i<this.getHeight()/20;i++){
			if(i%2==0){
				g.setColor(SwingResourceManager.getColor(50,29,34));
			}
			else{
				g.setColor(SwingResourceManager.getColor(100,129,33));
			}
			g.fillRect(0,i*20,20,this.getWidth());
		}
		g.setColor(SwingResourceManager.getColor(100,129,33));
		g.fillRect(0,60,200,200);
		super.paintComponent(g);
	}
}
class CustomViewportUI extends ViewportUI{
	 public void paint(Graphics g, JComponent c) {
		 int rowHeight=16;
		 for(int i=0;i<=c.getSize().height/rowHeight;i++){
				if(i%2==0){
					g.setColor(SwingResourceManager.getColor(50,50,50));
				}
				else{
					g.setColor(SwingResourceManager.getColor(150,150,150));
				}
				g.fillRect(0,i*rowHeight,c.getSize().width,rowHeight);
			}
			super.paint(g,c);
	}
}
